﻿using System;

namespace LcfContols
{
    /// <summary>
    /// 分页对象
    /// </summary>
    [Serializable]
    public class Page
    {
        /// <summary>
        /// 当前页
        /// </summary>
        private uint _nowPage;

        /// <summary>
        /// 默认每页条数
        /// </summary>
        private uint _defaultLength = 50;

        /// <summary>
        /// 所有条数
        /// </summary>
        public ulong rows { get; set; }

        /// <summary>
        /// 当前页
        /// </summary>
        public uint nowPage
        {
            get
            {
                return _nowPage;
            }
            set
            {
                if (value > allPage)
                    _nowPage = allPage;
                else if (value < 1)
                    _nowPage = 0;
                else
                    _nowPage = value;
            }
        }

        /// <summary>
        /// 总页数
        /// </summary>
        public uint allPage
        {
            get
            {
                if (_defaultLength == 0)
                    return 0;
                return Convert.ToUInt32(Math.Ceiling(rows * 1.0 / _defaultLength));
            }
        }

        /// <summary>
        /// 每页数据条数
        /// </summary>
        public uint pageLength {
            get
            {
                if (_defaultLength == 0)
                    _defaultLength = 50;
                return _defaultLength;
            }
            set
            {
                _defaultLength = value;
            }
        }

        /// <summary>
        /// 开始条数，用于数据库
        /// </summary>
        public uint start
        {
            get
            {
                return nowPage * _defaultLength;
            }
        }

    }
}
